package org.rudenko.horo;

import java.net.*;
import java.util.regex.*;
import java.io.*;

public class GetHoroscop {
	

	public static String get(String urlparam) {	
		final String[] PAT = {
/*what*/				"<div style=\"t_ext-align\\:justify; \\/\\/padding-right: 30px; \">(.*)<br>        <br>",
/*who*/					"<font class=hdr><b>(.*) \\S+ \\S+<\\/b><\\/font><br>            <font",
/*when*/				"<font color=\"#6f6f6f\"><b><i>(.*)<\\/i><\\/b><\\/font><br> " };
		String result[] = new String[PAT.length];
		URL url = null;
		HttpURLConnection urlConnection = null;
		String u = "http://horoscopes.rambler.ru/index.html?"+urlparam;
		try {
			url = new URL(u);
			urlConnection = (HttpURLConnection) url.openConnection();
			InputStream in = new BufferedInputStream(
					urlConnection.getInputStream());
			StringBuilder sb = new StringBuilder();
			BufferedReader r = new BufferedReader(new InputStreamReader(in,
					"Cp1251"));
			for (String line = r.readLine(); line != null; line = r.readLine()) {
				sb.append(line);
			}
			in.close();

			Pattern p;
			Matcher m;

			for (int i = 0; i < PAT.length; i++) {
				p = Pattern.compile(PAT[i], Pattern.MULTILINE);
				m = p.matcher(sb.toString());
				while (m.find()) {
					result[i] = android.text.Html.fromHtml(m.group(1)).toString();
				}
			}
		} catch (MalformedURLException e) {
			return "null";
		} catch (IOException e) {
			return "null";
		}

		return result[1]+"\n"+result[2]+"\n\n"+result[0];
	}
		
}
